﻿#include <string.h>
#include <Includes.h>
#include <DRV_EC600S.h>
#include <DRV_EC600S_Usim.h>
#include <OPP_Debug.h>

T_MUTEX usimMux;
EN_USIM_ST g_enUsimSt=USIM_UNKNOW;


int Ec600sUsimInit()
{
    MUTEX_CREATE(usimMux);
    return 0;
}

void Ec600sUsimStSet(EN_USIM_ST st)
{
    MUTEX_LOCK(usimMux, MUTEX_WAIT_ALWAYS);
    g_enUsimSt=st;
    MUTEX_UNLOCK(usimMux);
}

EN_USIM_ST Ec600sUsimStGet()
{
    EN_USIM_ST st;

    MUTEX_LOCK(usimMux, MUTEX_WAIT_ALWAYS);
    st=g_enUsimSt;
    MUTEX_UNLOCK(usimMux);
    return st;
}

int Ec600sUsimLoop()
{
    static U32 tick=0;

    //测试模式不重启
    if(Ec600sStGet()==EC600S_DEV_TEST)
    {
        tick=0;
        return 0;
    }

    //设备未就绪
    if(Ec600sStGet()<EC600S_DEV_PWRON)
    {
        tick=0;
        return 0;
    }
    //sim卡就绪
    if(Ec600sUsimStGet()==USIM_READY)
    {
        tick=0;
        return 0;
    }

    if(tick==0)
        tick=OppTickCountGet();

    if(OppTickCountGet()-tick>USIMTO)
    {
        if(USIM_READY!=Ec600sUsimStGet())
        {
            if(Ec600sRstTypeGet()==EC600S_RST_NONE)
                Ec600sReset(EC600S_RST_CMD);
            tick=0;
        }
    }
    return 0;
}

